using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;

using EcsSiteDSTableAdapters;


namespace SiteECSWeb
{
    [DataObject]
    public class EcsEmployeeBLL
    {

        private EcsEmployeeTableAdapter _EcsEmployeeAdapter = null;

        protected EcsEmployeeTableAdapter EcsEmployeeAdapter
        {
            get
            {
                if (_EcsEmployeeAdapter == null)
                {
                    _EcsEmployeeAdapter = new EcsEmployeeTableAdapter();
                }
                return _EcsEmployeeAdapter;
            }
        }



        #region SelecEcsEmployee
        public int GetTotalNumberOfEmployees()
        {
            Nullable<int> totalNull = (Nullable<int>)EcsEmployeeAdapter.TotalNumberOfEcsEmploye();
            return totalNull.GetValueOrDefault();
        }
        public int GetTotalNumberOfEmployees(string FilterStr)
        {
            if (string.IsNullOrEmpty(FilterStr))
            {
                return GetTotalNumberOfEmployees();
            }
            else
            {
                Nullable<int> totalNull = (Nullable<int>)EcsEmployeeAdapter.
                TotalNumberOfEcsEmployeByFilterExp(FilterStr);
                return totalNull.GetValueOrDefault();
            }
        }

        // Get All Ecs Employee Data//
        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public EcsSiteDS.EcsEmployeeDataTable GetALLEcsEmployee()
        {
            return EcsEmployeeAdapter.GetEcsEmployeRowRankData();

        }

        // Get Ecs Employee Data With Ecsemp ID//
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.EcsEmployeeDataTable GetEcsEmployeeByEmpID(int Ecsemp_id)
        {
            return EcsEmployeeAdapter.GetEcsEmployeDataByEcsempId(Ecsemp_id);
            //if (Ecsemp_id <= 0)
            //{
            //    return EcsEmployeeAdapter.GetEcsEmployeRowRankData();
            //}
            //else
            //{
               
            //}
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.EcsEmployeeDataTable GetEcsEmployeeByEmpEmail(string Ecsemp_email)
        {
            return EcsEmployeeAdapter.GetEcsEmployeDataByEcsempEmail(Ecsemp_email);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.EcsEmployeeDataTable GetEcsEmployeeCurrentByCityID(int CityID)
        {
            return EcsEmployeeAdapter.GetEcsEmployeOffDataByCityID(CityID);
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.EcsEmployeeDataTable GetEcsEmployeeCurrentByDepID(int DepID)
        {
            return EcsEmployeeAdapter.GetEmployeDepDataByDepID(DepID);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.EcsEmployeeDataTable GetEmployeesByGridPagesFilerExp(int startRowIndex,
            int maximumRows, string sortExpression)
        {
            return EcsEmployeeAdapter.GetEcsEmployeDataByGridPages(startRowIndex, maximumRows, sortExpression);
        }


        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.EcsEmployeeDataTable GetEmployeesByGridPagesFilerExp(int startRowIndex, int maximumRows,
            string FilterStr, string sortExpression)
        {
            if (string.IsNullOrEmpty(FilterStr))
            {
                return
                    GetEmployeesByGridPagesFilerExp(startRowIndex, maximumRows, sortExpression);
            }
            else
            {
                return EcsEmployeeAdapter.GetEcsEmployeDataByGridPagesFilterExp
                    (startRowIndex, maximumRows, FilterStr, sortExpression);
            }
        }

        #endregion

        #region InserEcsEmployee
        [DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
        public int AddEcsEmployee(string Ecsemp_nmar, string Ecsemp_gender, DateTime? Ecsemp_BirthDate, string Ecsemp_addrsarEg,
            string Ecsemp_nmen, string Ecsemp_addrsenEg, string Ecsemp_phnEg, string Ecsemp_email,
            string Ecsemp_mobilEg, DateTime? Ecsemp_SJoinDate, string Ecsemp_JoinCode, 
            DateTime? Ecsemp_EJoinDate, string Ecsemp_phnOff,
            string Ecsemp_mobilOff, string Ecsemp_addrsOff,
            string Ecsemp_InactivityReason, string Ecsemp_Picturepath, int EmpType_ID)
        {
      
            EcsSiteDS.EcsEmployeeDataTable EcsEmployeeTable = new EcsSiteDS.EcsEmployeeDataTable();
            EcsSiteDS.EcsEmployeeRow EcsEmployee_Row = EcsEmployeeTable.NewEcsEmployeeRow();
            
            if (string.IsNullOrEmpty(Ecsemp_gender)) EcsEmployee_Row.SetEcsemp_genderNull(); else EcsEmployee_Row.Ecsemp_gender = Ecsemp_gender;
            if (Ecsemp_BirthDate == null) EcsEmployee_Row.SetEcsemp_BirthDateNull(); else EcsEmployee_Row.Ecsemp_BirthDate = Ecsemp_BirthDate.Value;
            if (string.IsNullOrEmpty(Ecsemp_addrsarEg )) EcsEmployee_Row.SetEcsemp_addrsarEgNull(); else EcsEmployee_Row.Ecsemp_addrsarEg = Ecsemp_addrsarEg;
            if (string.IsNullOrEmpty(Ecsemp_nmen )) EcsEmployee_Row.SetEcsemp_nmenNull(); else EcsEmployee_Row.Ecsemp_nmen = Ecsemp_nmen;
            if (string.IsNullOrEmpty(Ecsemp_addrsenEg )) EcsEmployee_Row.SetEcsemp_addrsenEgNull(); else EcsEmployee_Row.Ecsemp_addrsenEg = Ecsemp_addrsenEg;
            if (string.IsNullOrEmpty(Ecsemp_phnEg )) EcsEmployee_Row.SetEcsemp_phnEgNull(); else EcsEmployee_Row.Ecsemp_phnEg = Ecsemp_phnEg;
            if (string.IsNullOrEmpty(Ecsemp_email )) EcsEmployee_Row.SetEcsemp_emailNull(); else EcsEmployee_Row.Ecsemp_email = Ecsemp_email;
            if (string.IsNullOrEmpty(Ecsemp_mobilEg )) EcsEmployee_Row.SetEcsemp_mobilEgNull(); else EcsEmployee_Row.Ecsemp_mobilEg = Ecsemp_mobilEg;
            if (Ecsemp_SJoinDate == null) EcsEmployee_Row.SetEcsemp_SJoinDateNull(); else EcsEmployee_Row.Ecsemp_SJoinDate = Ecsemp_SJoinDate.Value;
            if (Ecsemp_EJoinDate == null) EcsEmployee_Row.SetEcsemp_EJoinDateNull(); else EcsEmployee_Row.Ecsemp_EJoinDate = Ecsemp_EJoinDate.Value;
            if (string.IsNullOrEmpty(Ecsemp_InactivityReason)) EcsEmployee_Row.SetEcsemp_InactivityReasonNull(); else EcsEmployee_Row.Ecsemp_InactivityReason = Ecsemp_InactivityReason;
            if (string.IsNullOrEmpty(Ecsemp_Picturepath)) EcsEmployee_Row.SetEcsemp_PicturepathNull(); else EcsEmployee_Row.Ecsemp_Picturepath = Ecsemp_Picturepath;
            if (string.IsNullOrEmpty(Ecsemp_JoinCode)) EcsEmployee_Row.SetEcsemp_JoinCodeNull(); else EcsEmployee_Row.Ecsemp_JoinCode = Ecsemp_JoinCode;
            if (string.IsNullOrEmpty(Ecsemp_phnOff )) EcsEmployee_Row.SetEcsemp_phnOffNull(); else EcsEmployee_Row.Ecsemp_phnOff = Ecsemp_phnOff;
            if (string.IsNullOrEmpty(Ecsemp_mobilOff )) EcsEmployee_Row.SetEcsemp_mobilEgNull(); else EcsEmployee_Row.Ecsemp_mobilOff = Ecsemp_mobilOff;
            if (string.IsNullOrEmpty(Ecsemp_addrsOff )) EcsEmployee_Row.SetEcsemp_addrsOffNull(); else EcsEmployee_Row.Ecsemp_addrsOff = Ecsemp_addrsOff;


            EcsEmployee_Row.Ecsemp_nmar = Ecsemp_nmar;
            EcsEmployee_Row.EmpType_ID = EmpType_ID;

            EcsEmployeeTable.AddEcsEmployeeRow(EcsEmployee_Row);
            
            int rowsAffected = EcsEmployeeAdapter.Update(EcsEmployeeTable);

            return (rowsAffected == 1)? EcsEmployee_Row.Ecsemp_id : -1;

        }



        #endregion

        #region UpdateECsEmployee

        [DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
        public bool UpdateEcsEmployee(string Ecsemp_nmar, string Ecsemp_gender, 
            DateTime? Ecsemp_BirthDate, string Ecsemp_addrsarEg,
            string Ecsemp_nmen, string Ecsemp_addrsenEg, string Ecsemp_phnEg, string Ecsemp_email,
            string Ecsemp_mobilEg, DateTime? Ecsemp_SJoinDate, string Ecsemp_JoinCode, 
            DateTime? Ecsemp_EJoinDate, string Ecsemp_phnOff, string Ecsemp_mobilOff, string Ecsemp_addrsOff,
            string Ecsemp_InactivityReason, string Ecsemp_Picturepath, int EmpType_ID, int Ecsemp_id)
        {

            EcsSiteDS.EcsEmployeeDataTable EcsEmployeeTable = 
                EcsEmployeeAdapter.GetEcsEmployeDataByEcsempId(Ecsemp_id);
            
            if (EcsEmployeeTable.Count == 0)
            {
                return false;
            }

            EcsSiteDS.EcsEmployeeRow EcsEmployee_Row = EcsEmployeeTable[0];
            //if (Ecsemp_gender == null) EcsEmployee_Row.SetEcsemp_genderNull(); else EcsEmployee_Row.Ecsemp_gender = Ecsemp_gender;
            //if (Ecsemp_BirthDate == null) EcsEmployee_Row.SetEcsemp_BirthDateNull(); else EcsEmployee_Row.Ecsemp_BirthDate = Ecsemp_BirthDate.Value;
            //if (Ecsemp_addrsarEg == null) EcsEmployee_Row.SetEcsemp_addrsarEgNull(); else EcsEmployee_Row.Ecsemp_addrsarEg = Ecsemp_addrsarEg;
            //if (Ecsemp_nmen == null) EcsEmployee_Row.SetEcsemp_nmenNull(); else EcsEmployee_Row.Ecsemp_nmen = Ecsemp_nmen;
            //if (Ecsemp_addrsenEg == null) EcsEmployee_Row.SetEcsemp_addrsenEgNull(); else EcsEmployee_Row.Ecsemp_addrsenEg = Ecsemp_addrsenEg;
            //if (Ecsemp_phnEg == null) EcsEmployee_Row.SetEcsemp_addrsenEgNull(); else EcsEmployee_Row.Ecsemp_phnEg = Ecsemp_phnEg;
            //if (Ecsemp_email == null) EcsEmployee_Row.SetEcsemp_emailNull(); else EcsEmployee_Row.Ecsemp_email = Ecsemp_email;
            //if (Ecsemp_mobilEg == null) EcsEmployee_Row.SetEcsemp_mobilEgNull(); else EcsEmployee_Row.Ecsemp_mobilEg = Ecsemp_mobilEg;
            //if (Ecsemp_SJoinDate == null) EcsEmployee_Row.SetEcsemp_SJoinDateNull(); else EcsEmployee_Row.Ecsemp_SJoinDate = Ecsemp_SJoinDate.Value;
            //if (Ecsemp_EJoinDate == null) EcsEmployee_Row.SetEcsemp_EJoinDateNull(); else EcsEmployee_Row.Ecsemp_EJoinDate = Ecsemp_EJoinDate.Value;
            //if (string.IsNullOrEmpty(Ecsemp_InactivityReason )) EcsEmployee_Row.SetEcsemp_InactivityReasonNull(); else EcsEmployee_Row.Ecsemp_InactivityReason = Ecsemp_InactivityReason;
            //if (string.IsNullOrEmpty(Ecsemp_Picturepath)) EcsEmployee_Row.SetEcsemp_PicturepathNull(); else EcsEmployee_Row.Ecsemp_Picturepath = Ecsemp_Picturepath;
            //if (Ecsemp_mobilOff == null) EcsEmployee_Row.SetEcsemp_mobilEgNull(); else EcsEmployee_Row.Ecsemp_mobilOff = Ecsemp_mobilOff;
            //if (Ecsemp_addrsOff == null) EcsEmployee_Row.SetEcsemp_addrsOffNull(); else EcsEmployee_Row.Ecsemp_addrsOff = Ecsemp_addrsOff;
            //if (string.IsNullOrEmpty(Ecsemp_JoinCode)) EcsEmployee_Row.SetEcsemp_JoinCodeNull(); else EcsEmployee_Row.Ecsemp_JoinCode = Ecsemp_JoinCode;
            if (string.IsNullOrEmpty(Ecsemp_gender)) EcsEmployee_Row.SetEcsemp_genderNull(); else EcsEmployee_Row.Ecsemp_gender = Ecsemp_gender;
            if (Ecsemp_BirthDate == null) EcsEmployee_Row.SetEcsemp_BirthDateNull(); else EcsEmployee_Row.Ecsemp_BirthDate = Ecsemp_BirthDate.Value;
            if (string.IsNullOrEmpty(Ecsemp_addrsarEg)) EcsEmployee_Row.SetEcsemp_addrsarEgNull(); else EcsEmployee_Row.Ecsemp_addrsarEg = Ecsemp_addrsarEg;
            if (string.IsNullOrEmpty(Ecsemp_nmen)) EcsEmployee_Row.SetEcsemp_nmenNull(); else EcsEmployee_Row.Ecsemp_nmen = Ecsemp_nmen;
            if (string.IsNullOrEmpty(Ecsemp_addrsenEg)) EcsEmployee_Row.SetEcsemp_addrsenEgNull(); else EcsEmployee_Row.Ecsemp_addrsenEg = Ecsemp_addrsenEg;
            if (string.IsNullOrEmpty(Ecsemp_phnEg)) EcsEmployee_Row.SetEcsemp_phnEgNull(); else EcsEmployee_Row.Ecsemp_phnEg = Ecsemp_phnEg;
            if (string.IsNullOrEmpty(Ecsemp_email)) EcsEmployee_Row.SetEcsemp_emailNull(); else EcsEmployee_Row.Ecsemp_email = Ecsemp_email;
            if (string.IsNullOrEmpty(Ecsemp_mobilEg)) EcsEmployee_Row.SetEcsemp_mobilEgNull(); else EcsEmployee_Row.Ecsemp_mobilEg = Ecsemp_mobilEg;
            if (Ecsemp_SJoinDate == null) EcsEmployee_Row.SetEcsemp_SJoinDateNull(); else EcsEmployee_Row.Ecsemp_SJoinDate = Ecsemp_SJoinDate.Value;
            if (Ecsemp_EJoinDate == null) EcsEmployee_Row.SetEcsemp_EJoinDateNull(); else EcsEmployee_Row.Ecsemp_EJoinDate = Ecsemp_EJoinDate.Value;
            if (string.IsNullOrEmpty(Ecsemp_InactivityReason)) EcsEmployee_Row.SetEcsemp_InactivityReasonNull(); else EcsEmployee_Row.Ecsemp_InactivityReason = Ecsemp_InactivityReason;
            if (string.IsNullOrEmpty(Ecsemp_Picturepath)) EcsEmployee_Row.SetEcsemp_PicturepathNull(); else EcsEmployee_Row.Ecsemp_Picturepath = Ecsemp_Picturepath;
            if (string.IsNullOrEmpty(Ecsemp_JoinCode)) EcsEmployee_Row.SetEcsemp_JoinCodeNull(); else EcsEmployee_Row.Ecsemp_JoinCode = Ecsemp_JoinCode;
            if (string.IsNullOrEmpty(Ecsemp_phnOff)) EcsEmployee_Row.SetEcsemp_phnOffNull(); else EcsEmployee_Row.Ecsemp_phnOff = Ecsemp_phnOff;
            if (string.IsNullOrEmpty(Ecsemp_mobilOff)) EcsEmployee_Row.SetEcsemp_mobilEgNull(); else EcsEmployee_Row.Ecsemp_mobilOff = Ecsemp_mobilOff;
            if (string.IsNullOrEmpty(Ecsemp_addrsOff)) EcsEmployee_Row.SetEcsemp_addrsOffNull(); else EcsEmployee_Row.Ecsemp_addrsOff = Ecsemp_addrsOff;

            EcsEmployee_Row.Ecsemp_nmar = Ecsemp_nmar;
            EcsEmployee_Row.EmpType_ID = EmpType_ID;

            int rowsAffected = EcsEmployeeAdapter.Update(EcsEmployee_Row);

            return rowsAffected == 1;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
        public bool UpdateEcsEmployee(DateTime? Ecsemp_EJoinDate, 
            string Ecsemp_InactivityReason, int Ecsemp_id)
        {

            EcsSiteDS.EcsEmployeeDataTable EcsEmployeeTable =
                EcsEmployeeAdapter.GetEcsEmployeDataByEcsempId(Ecsemp_id);

            if (EcsEmployeeTable.Count == 0)
            {
                return false;
            }

            EcsSiteDS.EcsEmployeeRow EcsEmployee_Row = EcsEmployeeTable[0];

            if (Ecsemp_EJoinDate == null) EcsEmployee_Row.SetEcsemp_EJoinDateNull(); else EcsEmployee_Row.Ecsemp_EJoinDate = Ecsemp_EJoinDate.Value;            
            if (string.IsNullOrEmpty(Ecsemp_InactivityReason)) EcsEmployee_Row.SetEcsemp_InactivityReasonNull(); 
            else EcsEmployee_Row.Ecsemp_InactivityReason = Ecsemp_InactivityReason;

            int rowsAffected = EcsEmployeeAdapter.Update(EcsEmployee_Row);

            return rowsAffected == 1;
        }

        #endregion

        #region DeleteEcsEmloyee

        [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
        public bool DeleteEcsEmployee(int Ecsemp_id)
        {

            EcsSiteDS.EcsEmployeeDataTable EcsEmployeeTable =
                EcsEmployeeAdapter.GetEcsEmployeDataByEcsempId(Ecsemp_id);

            if (EcsEmployeeTable.Count == 0)
            {
                return false;
            }
            
            EcsSiteDS.EcsEmployeeRow EcsEmployee_Row = EcsEmployeeTable[0];

            MembershipUser  mUser = Membership.GetUser
                (Membership.GetUserNameByEmail(EcsEmployee_Row.Ecsemp_email) ?? string.Empty);

            if (mUser != null)
            {
                Membership.DeleteUser(mUser.UserName, true);
            }
            if (!EcsEmployee_Row.IsEcsemp_PicturepathNull())
            {
                System.IO.File.Delete(HttpContext.Current.Server.MapPath(Globals.Settings.UploadInfo.RootAttachFolder
                         + EcsEmployee_Row.Ecsemp_Picturepath));
            }
            
            int rowsAffected = EcsEmployeeAdapter.Delete(Ecsemp_id);
            return rowsAffected == 1;
        }
        #endregion

    }
}